package com.semidata.trp.report.http;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.QueryStringDecoder;

import com.semidata.rtd.core.http.server.AbstractDispatchHandler;
import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.report.report.NRTReport;
import com.semidata.trp.report.report.SortLogs;

public class NRTReportHandler extends AbstractDispatchHandler {

	public NRTReportHandler(ChannelHandlerContext ctx, HttpRequest request) {
		super(ctx, request);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handle(HttpSession session) {
		// TODO Auto-generated method stub
		HttpRequest request = session.getRequest();
		String requestUri = request.getUri();
		QueryStringDecoder decoder = new QueryStringDecoder(requestUri);
		final Map<String, List<String>> results = decoder.parameters();
		if (results.get("d") == null) {
			session.getResponse().error(HttpResponseStatus.BAD_REQUEST);
		} else {
			final String date = results.get("d").get(0);
			Thread thread = new Thread(new Runnable() {
				public void run() {
					if (results.get("i") == null) {
						/*
						 * final MakeTestLog miniHeap = new MakeTestLog(); final
						 * String date = results.get("d").get(0); try {
						 * miniHeap.makeLogs(date); } catch (Exception e) {
						 * Logger.log("ERROR", date); Logger.log("ERROR",
						 * e.getMessage()); }
						 */
					} else if (results.get("t") == null) {
						Thread thread = new Thread(new Runnable() {
							public void run() {
								long start = System.currentTimeMillis(), end;
								SortLogs test = new SortLogs();
								String minute;
								SimpleDateFormat dateformat = new SimpleDateFormat(
										"yyyyMMddHHmm");
								Calendar c = new GregorianCalendar();
								c.set(Integer.valueOf(date.substring(0, 4)),
										Integer.valueOf(date.substring(4, 6)) - 1,
										Integer.valueOf(date.substring(6)), 0,
										0);
								boolean ignore = false;
								if (results.get("i") != null) {
									ignore = Boolean.valueOf(results.get("i")
											.get(0));
								}
								try {
									test.setAllList(date);
									Logger.log("DEBUG", "set list finished");
									end = System.currentTimeMillis();
									Logger.log(
											"DEBUG",
											"cost : "
													+ String.valueOf((end - start) / 1000)
													+ "s");
									start = end;

									for (int i = 0; i < 1440; i++) {
										minute = dateformat.format(c.getTime());
										try {
											test.setFilesByMinute(minute);
										} catch (Exception e) {
											Logger.log("ERROR", e.getMessage());
											Logger.log("ERROR", "sort error : "
													+ minute);
											if (!ignore) {
												continue;
											}
										} finally {
											c.set(Calendar.MINUTE,
													c.get(Calendar.MINUTE) + 1);
										}
									}
									Logger.log("DEBUG", "all minutes sort over");
									end = System.currentTimeMillis();
									Logger.log(
											"DEBUG",
											"cost : "
													+ String.valueOf((end - start) / 1000)
													+ "s");

								} catch (Exception e) {
									Logger.log("ERROR", date);
									Logger.log("ERROR", e.getMessage());
								}
								test.finalSort(date);
								Logger.log("DEBUG", "final sort over");
								end = System.currentTimeMillis();
								Logger.log(
										"DEBUG",
										"cost : "
												+ String.valueOf((end - start) / 1000)
												+ "s");
							}
						});
						thread.start();
					} else {
						final NRTReport reportor = new NRTReport();
						final String date = results.get("d").get(0);
						final Integer type = Integer.valueOf(results.get("t")
								.get(0));
						boolean ignore = false;
						if (results.get("i") != null) {
							ignore = Boolean.valueOf(results.get("i").get(0));
						}
						try {
							reportor.setFileList(date, type, ignore);
						} catch (Exception e) {
							Logger.log("ERROR", date);
							Logger.log("ERROR", e.getMessage());
						}
					}
				}
			});
			thread.start();
			session.getResponse().end("start");
			try {
				thread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
